//
//  BaseModel.m
//  OCStarterProject
//
//  Created by huaya on 2025/10/2.
//

#import "BaseModel.h"

@implementation BaseModel

#pragma mark - Initialization

- (instancetype)init {
    self = [super init];
    if (self) {
        _modelId = @"";
        _createdAt = [NSDate date];
        _updatedAt = [NSDate date];
    }
    return self;
}

- (instancetype)initWithDictionary:(NSDictionary *)dictionary {
    self = [self init];
    if (self) {
        [self setValuesForKeysWithDictionary:dictionary];
    }
    return self;
}

#pragma mark - NSCoding

- (instancetype)initWithCoder:(NSCoder *)coder {
    self = [super init];
    if (self) {
        _modelId = [coder decodeObjectForKey:@"modelId"];
        _createdAt = [coder decodeObjectForKey:@"createdAt"];
        _updatedAt = [coder decodeObjectForKey:@"updatedAt"];
    }
    return self;
}

- (void)encodeWithCoder:(NSCoder *)coder {
    [coder encodeObject:self.modelId forKey:@"modelId"];
    [coder encodeObject:self.createdAt forKey:@"createdAt"];
    [coder encodeObject:self.updatedAt forKey:@"updatedAt"];
}

#pragma mark - NSCopying

- (id)copyWithZone:(NSZone *)zone {
    BaseModel *copy = [[[self class] allocWithZone:zone] init];
    copy.modelId = [self.modelId copy];
    copy.createdAt = [self.createdAt copy];
    copy.updatedAt = [self.updatedAt copy];
    return copy;
}

#pragma mark - Dictionary Conversion

- (NSDictionary *)toDictionary {
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    
    if (self.modelId) {
        dict[@"modelId"] = self.modelId;
    }
    if (self.createdAt) {
        dict[@"createdAt"] = self.createdAt;
    }
    if (self.updatedAt) {
        dict[@"updatedAt"] = self.updatedAt;
    }
    
    return [dict copy];
}

- (NSString *)toJSONString {
    NSDictionary *dict = [self toDictionary];
    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:0 error:&error];
    if (error) {
        NSLog(@"Model to JSON error: %@", error);
        return @"";
    }
    return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}

+ (instancetype)modelFromJSONString:(NSString *)jsonString {
    if (!jsonString || jsonString.length == 0) {
        return nil;
    }
    
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *error = nil;
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
    
    if (error) {
        NSLog(@"JSON to Model error: %@", error);
        return nil;
    }
    
    return [[self alloc] initWithDictionary:dict];
}

#pragma mark - Array Conversion

+ (NSArray *)dictionaryArrayFromModelArray:(NSArray<BaseModel *> *)models {
    NSMutableArray *dictArray = [NSMutableArray array];
    for (BaseModel *model in models) {
        [dictArray addObject:[model toDictionary]];
    }
    return [dictArray copy];
}

+ (NSArray *)modelArrayFromDictionaryArray:(NSArray<NSDictionary *> *)dictionaries {
    NSMutableArray *modelArray = [NSMutableArray array];
    for (NSDictionary *dict in dictionaries) {
        BaseModel *model = [[self alloc] initWithDictionary:dict];
        [modelArray addObject:model];
    }
    return [modelArray copy];
}

#pragma mark - KVC Safe

- (void)setValue:(id)value forUndefinedKey:(NSString *)key {
    NSLog(@"Warning: %@ does not have key: %@", NSStringFromClass([self class]), key);
}

- (id)valueForUndefinedKey:(NSString *)key {
    NSLog(@"Warning: %@ does not have key: %@", NSStringFromClass([self class]), key);
    return nil;
}

@end
